package com.sky.zzh.devicemanager;

import org.json.JSONObject;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;

import com.next.intf.ITaskListener;
import com.next.net.SHPost;
import com.next.net.SHPut;
import com.next.net.SHTask;
import com.sky.widget.SHDialog;
import com.sky.widget.sweetdialog.SweetDialog;
import com.sky.zzh.BaseFragment;
import com.sky.zzh.R;
import com.sky.zzh.home.LoginActivity;
import com.sky.zzh.util.CommonUtil;
import com.sky.zzh.util.ConfigDefinition;
import com.sky.zzh.util.UserInfoManager;
import com.sky.zzh.view.SHEditTextNoClear;
/**
 * 激活设备
 * @author skypan
 *
 */
public class ActiveDeviceFragment extends BaseFragment implements ITaskListener{

	private SHEditTextNoClear mEtWifiName,mEtWifiPwd;
	private Button mBtnActive,mBtnBack;
	
	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onViewCreated(view, savedInstanceState);
		mEtWifiName = (SHEditTextNoClear) view.findViewById(R.id.et_net_name);
		mEtWifiPwd = (SHEditTextNoClear) view.findViewById(R.id.et_net_pwd);
		mBtnActive = (Button) view.findViewById(R.id.btn_active);
		mBtnBack = (Button) view.findViewById(R.id.btn_back);
		
		setListeners();
		
		mEtWifiName.setText(CommonUtil.getWifiSSID());
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View view = inflater.inflate(R.layout.fragment_active, container, false);
		return view;
	}
	
	private void setListeners(){
		mBtnBack.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				getActivity().finish();
			}
		});
		
		mBtnActive.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				SHDialog.ShowProgressDiaolg(getActivity());
				SHPut activeTask = new SHPut();
				activeTask.setUrl(ConfigDefinition.URL+"user/ActivateUserDevice");
				activeTask.setListener(ActiveDeviceFragment.this);
				activeTask.getTaskArgs().put("UserID", UserInfoManager.getInstance().getUserId());
				activeTask.getTaskArgs().put("UUID", getActivity().getIntent().getStringExtra("UUID"));
				activeTask.start();
			}
		});
	}

	@Override
	public void onTaskFinished(SHTask task) throws Exception {
		// TODO Auto-generated method stub
		SHDialog.dismissProgressDiaolg();
		System.out.println(task.getResult());
		JSONObject json = new JSONObject(task.getResult().toString());
		if ("200".equals(json.optString("ReturnCode"))) {
			getActivity().finish();
		}else{
			new SweetDialog(getActivity(), SweetDialog.ERROR_TYPE).setTitleText("提示").setContentText(json.optString("ErrorMessage")).show();
		}
	}

	@Override
	public void onTaskFailed(SHTask task) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onTaskUpdateProgress(SHTask task, int count, int total) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onTaskTry(SHTask task) {
		// TODO Auto-generated method stub
		
	}

}
